// pages/order/order.js
var api = require('../../service/api.js')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        orderStatistics: [
            { label: '待付款', status: 0, icon: 'icon-money_icon' },
            { label: '待收货', status: 2, icon: 'icon-dingwei' },
            { label: '已完成', status: 3, icon: 'icon-ico_orderAdd' },
            { label: '已取消', status: -1, icon: 'icon-nodata' },
            { label: '全部订单', status: '', icon: 'icon-order' },
        ],
        searchForm:{
            phone: '18520314190',
            status: '',
            pageIndex: 1,
            pageSize: 10
        },
        orders:[],
    },

    tabChanged(e){
        //console.log(e);
        var tab = e.detail.tab;
        var status = tab.status ? parseInt(tab.status) : '';
        this.data.searchForm.status = status;
        this.getOrders();
    },

    getOrders(){
        console.log(this.data.searchForm);

        var search = this.data.searchForm;
        api.findUserOrderList(search.phone, search.status, search.pageIndex, search.pageSize).then(resp => {
            console.log(resp);
            var orders = resp.data.rows, orderCount = 0;

            orders.map(item => {
                orderCount = 0;
                item.prodList.map(prod=>{
                    orderCount += prod.prodCount;
                    if (prod.prodImage) {
                        prod.prodImages = prod.prodImage.split(',');
                        prod.firstProdImage = prod.prodImages[0];
                    }
                });
                item.orderCount = orderCount;
            });

            console.log('orders',orders);
            this.setData({
                orders: orders, 
                searchForm:this.data.searchForm
            });
        });
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        console.log('order-option', options);
        var status = options.status || '';
        this.setData({
            'searchForm.status' : status
        });
        this.getOrders();
    },

    orderDetail(event) {
        var order = event.currentTarget.dataset.order;
        wx.navigateTo({
            url: '/pages/order/detail/detail?orderCode=' + order.orderCode
        });
        console.log('orderDetail', order);
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    }
})